package com.zzr.jdk8date.demo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author: xiaomi
 * @date: 2021/9/4
 * @description:
 */
public class DateDemo {

    public static void printLocalDate() {
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(1);
        LocalDate plus2 = now.plusDays(2);
        //2021-09-04
        System.out.println(now);
        //2021-09-05
        System.out.println(plus);
        //2021-09-06
        System.out.println(plus2);
        System.out.println();
    }

    /**
     * 可以获取一天中开始和结束的时间
     */
    public static void printLocalTime() {
        LocalTime min = LocalTime.MIN;
        LocalTime max = LocalTime.MAX;
        //00:00
        System.out.println(min);
        //23:59:59.999999999
        System.out.println(max);
        System.out.println();
    }

    /**
     * 结合 LocalTime 和 LocalDate
     * 可以获取某天的开始时刻和最后时刻
     */
    public static void printLocalDateTimeCombine() {
        LocalDate now = LocalDate.now();

        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);
        String startFormat = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //2021-09-04 00:00:00
        System.out.println(startFormat);

        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(now, max);
        String endFormat = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //2021-09-04 23:59:59
        System.out.println(endFormat);

    }

    public static void main(String[] args) {
        DateDemo.printLocalDate();

        printLocalTime();

        printLocalDateTimeCombine();
    }
}
